<?php

namespace app\admin\controller;

use app\service\MsgService;
use think\Controller;
use app\common\model;
use app\common\model\Mall;
use app\common\model\Menu;

class Base extends Controller
{
    public $manager;
    public $managerModel;
    public $mall;
    public $mallModel;

    public $mall_id;

    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET,POST");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        $this->managerModel = new model\Manager;
        $this->mallModel = new Mall;
        $this->setConfig();
        $this->debugLogin();
        $this->checkLogin();
        //配置模板消息
    }

    protected function setConfig()
    {
        $configModel = new model\Config;
        config($configModel->getConfig());
    }


    public function debugLogin()
    {
        $id = 30; //3;
        $where = [
            'id' => $id
        ];
        $manager = $this->managerModel->getOne($where);
        session('manager', $manager);
        $this->manager = $manager;

        $this->mall_id = $this->manager['mall_id'];
    }

    protected function checkLogin()
    {
        $manager = session('manager');
        header('content-type:application/json;charset=utf-8');
        if (request()->action() == 'ticket') {
            return true;
        }
        if (!$manager) {
            //返回code==2 前端会跳转到登录页
            exit(json_encode([
                'code' => 2
            ]));
        }
        $where = [
            'id' => $manager['id']
        ];
        $manager = $this->managerModel->getOne($where);
        if (!$manager) {
            exit(json_encode([
                'code' => 2
            ]));
        }
        $this->manager = $manager;
        session('manager', $manager);
        if ($manager['mall_id']) {
            $mall = $this->mallModel->getOne([
                'id' => $manager['mall_id']
            ]);
            $this->mall = $mall;
            $this->mall_id = $mall['id'];
            session('mall', $mall);
        } else {
            $mall = $this->mallModel->getOne([
                'id' => 1
            ]);
            $this->mall = $mall;
            $this->mall_id = $mall['id'];
            session('mall', $mall);
        }

        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $where = [
            'level' => 4,
            'action' => $action,
            'controller' => $controller
        ];
        $menu = Menu::getOne($where);
        if ($manager['mall_id']) {
            $where['is_mall'] = 1;
        } else {
            $where['is_admin'] = 1;
        }
        if ($menu) {
            $where['id'] = $menu['id'];
            Menu::update($where);
        } else {
            Menu::create($where);
        }
    }

    /**
     * 返回
     * @param $code
     * @param $message
     * @param null $data
     * @param null $dataName
     */
    protected function ajax($code, $message, $data = null, $dataName = null)
    {
        if ($data != null && $data !== 0) {
            if ($dataName == null) {
                echo json_encode(array('code' => $code, 'msg' => $message, 'data' => $data));
                exit();
            } else {
                echo json_encode(array('code' => $code, 'msg' => $message, $dataName => $data));
                exit();
            }
        } else {
            echo json_encode(array('code' => $code, 'msg' => $message, 'data' => array()));
            exit();
        }
    }
}
